﻿Imports System.ComponentModel, System.Xml, Tools.Textt, ĐŘječnik.ImportExport
''' <summary>Represent information about dictionary</summary>
Public Class Dictionary
    ''' <summary>CTor</summary>
    ''' <remarks>This CTor is here in order to only derived class be able to create instance of this class</remarks>
    Protected Sub New()
    End Sub
#Region "Property"
    ''' <summary>Contains value of the <see cref="English1"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _English1 As String
    ''' <summary>Name of first language in English</summary>
    Public Property English1() As String
        Get
            Return _English1
        End Get
        Set(ByVal value As String)
            _English1 = value
        End Set
    End Property
    ''' <summary>Contains value of the <see cref="English2"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _English2 As String
    ''' <summary>Name of second language in english</summary>
    Public Property English2() As String
        Get
            Return _English2
        End Get
        Set(ByVal value As String)
            _English2 = value
        End Set
    End Property
    ''' <summary>Contains value of the <see cref="Lang1Code"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _Lang1Code As String
    ''' <summary>Code of first language</summary>
    Public Property Lang1Code() As String
        Get
            Return _Lang1Code
        End Get
        Set(ByVal value As String)
            _Lang1Code = value
        End Set
    End Property
    ''' <summary>Contains value of the <see cref="Lang2Code"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _Lang2Code As String
    ''' <summary>Code of second language</summary>
    Public Property Lang2Code() As String
        Get
            Return _Lang2Code
        End Get
        Set(ByVal value As String)
            _Lang2Code = value
        End Set
    End Property
    ''' <summary>Contains value of the <see cref="Native1"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _Native1 As String
    ''' <summary>Name of first language in first language</summary>
    Public Property Native1() As String
        Get
            Return _Native1
        End Get
        Set(ByVal value As String)
            _Native1 = value
        End Set
    End Property
    ''' <summary>Contains value of the <see cref="Native2"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _Native2 As String
    ''' <summary>Name of second language in second language</summary>
    Public Property Native2() As String
        Get
            Return _Native2
        End Get
        Set(ByVal value As String)
            _Native2 = value
        End Set
    End Property
    ''' <summary>Contains value of the <see cref="Other1"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _Other1 As String
    ''' <summary>Name of first language in second langauge</summary>
    Public Property Other1() As String
        Get
            Return _Other1
        End Get
        Set(ByVal value As String)
            _Other1 = value
        End Set
    End Property
    ''' <summary>Contains value of the <see cref="Other2"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _Other2 As String
    ''' <summary>Name of second language in first language</summary>
    Public Property Other2() As String
        Get
            Return _Other2
        End Get
        Set(ByVal value As String)
            _Other2 = value
        End Set
    End Property
    ''' <summary>Contains value of the <see cref="ICO"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _ICO As String
    ''' <summary>Filename of icon of dictionary</summary>
    ''' <returns>Flag combination is recomended for primary dictionary</returns>
    Public Property ICO() As String
        Get
            Return _ICO
        End Get
        Set(ByVal value As String)
            _ICO = value
        End Set
    End Property
    ''' <summary>Contains value of the <see cref="Name1"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _Name1 As String
    ''' <summary>Name of dictionary in first language</summary>
    Public Property Name1() As String
        Get
            Return _Name1
        End Get
        Set(ByVal value As String)
            _Name1 = value
        End Set
    End Property
    ''' <summary>Contains value of the <see cref="Name2"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _Name2 As String
    ''' <summary>Name of dictionary in second language</summary>
    Public Property Name2() As String
        Get
            Return _Name2
        End Get
        Set(ByVal value As String)
            _Name2 = value
        End Set
    End Property
    ''' <summary>Contains value of the <see cref="NameE"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _NameE As String
    ''' <summary>Name of dictionary in English</summary>
    Public Property NameE() As String
        Get
            Return _NameE
        End Get
        Set(ByVal value As String)
            _NameE = value
        End Set
    End Property
    ''' <summary>Contains value of the <see cref="Version"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _Version As String
    ''' <summary>Version of dictionary</summary>
    ''' <remarks>Version should consist of from 2 to 4 positive numbers separated by dot with no leading zeros</remarks>
    Public Property Version() As String
        Get
            Return _Version
        End Get
        Set(ByVal value As String)
            _Version = value
        End Set
    End Property
    ''' <summary>Contains value of the <see cref="Date"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _Date As Date
    ''' <summary>Date of issue of dictionary</summary>
    Public Property [Date]() As Date
        Get
            Return _Date
        End Get
        Set(ByVal value As Date)
            _Date = value
        End Set
    End Property
    ''' <summary>Contains value of the <see cref="Primary"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _Primary As PrimaryInfo
    ''' <summary>Information about primary dictionary</summary>
    ''' <remarks>Present only if dictionary is primary</remarks>
    Public Property Primary() As PrimaryInfo
        Get
            Return _Primary
        End Get
        Set(ByVal value As PrimaryInfo)
            _Primary = value
        End Set
    End Property

    ''' <summary>Contains value of the <see cref="Author"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _Author As AuthorInfo
    ''' <summary>Information about author of dictionary</summary>
    Public Property Author() As AuthorInfo
        Get
            Return _Author
        End Get
        Set(ByVal value As AuthorInfo)
            _Author = value
        End Set
    End Property
    ''' <summary>Contains value of the <see cref="Original"/> property</summary>
    <EditorBrowsable(EditorBrowsableState.Never)> Private _Original As OriginalAuthorInfo
    ''' <summary>Information of original author of dictionary</summary>
    ''' <remarks>Present only if original author differs from author. See <see cref="Author"/></remarks>
    Public Property Original() As OriginalAuthorInfo
        Get
            Return _Original
        End Get
        Set(ByVal value As OriginalAuthorInfo)
            _Original = value
        End Set
    End Property
#End Region
#Region "Classes"
    ''' <summary>Information about primary dictionary</summary>
    Public Class PrimaryInfo
        ''' <summary>Name of ICO1 XML attribute</summary>
        Protected Const attrICO1 As String = "ICO1"
        ''' <summary>Name of ICO2 XML attribute</summary>
        Protected Const attrICO2 As String = "ICO2"
        ''' <summary>Contains value of the <see cref="ICO1"/> property</summary>
        <EditorBrowsable(EditorBrowsableState.Never)> Private _ICO1 As String
        ''' <summary>Contains value of the <see cref="ICO2"/> property</summary>
        <EditorBrowsable(EditorBrowsableState.Never)> Private _ICO2 As String
        ''' <summary>Filename of icon of first language</summary>
        Public Property ICO1() As String
            Get
                Return _ICO1
            End Get
            Set(ByVal value As String)
                _ICO1 = value
            End Set
        End Property
        ''' <summary>Filename of icon of second language</summary>
        Public Property ICO2() As String
            Get
                Return _ICO2
            End Get
            Set(ByVal value As String)
                _ICO2 = value
            End Set
        End Property
        ''' <summary>CTor</summary>
        Public Sub New()
        End Sub
        ''' <summary>CTor from XML element &lt;Primary></summary>
        ''' <param name="Primary">The &lt;Primary> element to load data from</param>
        ''' <exception cref="ImportExport.ImportException">Attribute is missing</exception>
        Public Sub New(ByVal Primary As XmlElement)
            ICO1 = Primary.GetAttribute(attrICO1).Trim
            If ICO1 = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrICO1))
            ICO2 = Primary.GetAttribute(attrICO2).Trim
            If ICO2 = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrICO2))
        End Sub
    End Class
    ''' <summary>Information about author of XML dictionary</summary>
    Public Class AuthorInfo
        ''' <summary>Contains value of the <see cref="Nick"/> property</summary>
        <EditorBrowsable(EditorBrowsableState.Never)> Private _Nick As String
        ''' <summary>Contains value of the <see cref="FirstName"/> property</summary>
        <EditorBrowsable(EditorBrowsableState.Never)> Private _FirstName As String
        ''' <summary>Contains value of the <see cref="LastName"/> property</summary>
        <EditorBrowsable(EditorBrowsableState.Never)> Private _LastName As String
        ''' <summary>Contains value of the <see cref="EMail"/> property</summary>
        <EditorBrowsable(EditorBrowsableState.Never)> Private _EMail As String
        ''' <summary>Contains value of the <see cref="WWW"/> property</summary>
        <EditorBrowsable(EditorBrowsableState.Never)> Private _WWW As String
        ''' <summary>Contains value of the <see cref="Note"/> property</summary>
        <EditorBrowsable(EditorBrowsableState.Never)> Private _Note As String
        ''' <summary>Nick</summary>
        Public Property Nick() As String
            Get
                Return _Nick
            End Get
            Set(ByVal value As String)
                _Nick = value
            End Set
        End Property
        ''' <summary>First name</summary>
        Public Property FirstName() As String
            Get
                Return _FirstName
            End Get
            Set(ByVal value As String)
                _FirstName = value
            End Set
        End Property
        ''' <summary>Last name</summary>
        Public Property LastName() As String
            Get
                Return _LastName
            End Get
            Set(ByVal value As String)
                _LastName = value
            End Set
        End Property
        ''' <summary>@-mail</summary>
        Public Property EMail() As String
            Get
                Return _EMail
            End Get
            Set(ByVal value As String)
                _EMail = value
            End Set
        End Property
        ''' <summary>Web pages</summary>
        ''' <remarks>optional</remarks>
        Public Property WWW() As String
            Get
                Return _WWW
            End Get
            Set(ByVal value As String)
                _WWW = value
            End Set
        End Property
        ''' <summary>Note</summary>
        ''' <remarks>optionaly</remarks>
        Public Property Note() As String
            Get
                Return _Note
            End Get
            Set(ByVal value As String)
                _Note = value
            End Set
        End Property
        ''' <summary>CTor</summary>
        Public Sub New()
        End Sub
        ''' <summary>CTor from xml element &lt;author></summary>
        ''' <param name="author">The &lt;author> XMl element</param>
        ''' <exception cref="ImportExport.ImportException">Required XML attribute is missing</exception>
        Public Sub New(ByVal author As XmlElement)
            Nick = MTrim(author.GetAttribute(attrNick))
            If Nick = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrNick))
            FirstName = MTrim(author.GetAttribute(attrFirstName))
            If FirstName = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrFirstName))
            LastName = MTrim(author.GetAttribute(attrLastName))
            If LastName = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrLastName))
            EMail = MTrim(author.GetAttribute(attrEMail))
            If EMail = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrEMail))
            WWW = author.GetAttribute(attrWww).Trim
            Note = MTrim(author.GetAttribute(attrNote), True)
        End Sub
    End Class
    ''' <summary>Information about original author of dictionary data</summary>
    Public Class OriginalAuthorInfo : Inherits AuthorInfo
        ''' <summary>Contains value of the <see cref="Source"/> property</summary>
        <EditorBrowsable(EditorBrowsableState.Never)> Private _Source As String
        ''' <summary>Source (publication, website) of data</summary>
        Public Property Source() As String
            Get
                Return _Source
            End Get
            Set(ByVal value As String)
                _Source = value
            End Set
        End Property
        ''' <summary>CTor</summary>
        Public Sub New()
        End Sub
        ''' <summary>CTor from xml element &lt;original></summary>
        ''' <param name="original">The &lt;original> XMl element</param>
        ''' <remarks><seealso cref="AuthorInfo"/></remarks>
        ''' <exception cref="ImportExport.ImportException">Required XML attribute is missing</exception>
        Public Sub New(ByVal original As XmlElement)
            MyBase.New(original)
            Source = MTrim(original.GetAttribute(attrSource))
            If Source = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrSource))
        End Sub
    End Class
#End Region

    ''' <summary>Imports standard fields from standart &lt;info> XML element</summary>
    ''' <param name="Info">XML element to import data from</param>
    ''' <remarks>Does not load nested tags</remarks>
    ''' <exception cref="ImportExport.ImportException">Some XML attribute is missing or the Date attribute has bad format</exception>
    Protected Overridable Sub ImportInfo(ByVal Info As XmlElement)
        English1 = MTrim(Info.GetAttribute(attrEnglish1))
        If English1 = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrEnglish1))
        English2 = MTrim(Info.GetAttribute(attrEnglish2))
        If English2 = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrEnglish2))
        Lang1Code = MTrim(Info.GetAttribute(attrLang1Code))
        If Lang1Code = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrLang1Code))
        Lang2Code = MTrim(Info.GetAttribute(attrLang2Code))
        If Lang2Code = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrLang2Code))
        Native1 = MTrim(Info.GetAttribute(attrNative1))
        If Native1 = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrNative1))
        Native2 = MTrim(Info.GetAttribute(attrNative2))
        If Native2 = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrNative2))
        Other1 = MTrim(Info.GetAttribute(attrOther1))
        If Other1 = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrOther1))
        Other2 = MTrim(Info.GetAttribute(attrOther2))
        If Other2 = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrOther2))
        ICO = Info.GetAttribute(attrICO).Trim
        If ICO = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrICO))
        Name1 = MTrim(Info.GetAttribute(attrName1))
        If Name1 = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrName1))
        Name2 = MTrim(Info.GetAttribute(attrName2))
        If Name2 = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrName2))
        NameE = MTrim(Info.GetAttribute(attrNameE))
        If NameE = "" Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrNameE))
        Version = Info.GetAttribute(attrVersion)
        If Not Info.HasAttribute(attrDate) Then Throw New ImportExport.ImportException(String.Format(My.Resources.AttributeMissingOrEmpty, attrDate))
        Try
            [Date] = Info.GetAttribute(attrDate)
            [Date] = Date.Parse(Info.GetAttribute(attrDate), Globalization.CultureInfo.InvariantCulture.DateTimeFormat)
        Catch ex As Exception
            Throw New ImportExport.ImportException(String.Format(My.Resources.ErrorWhileConvertingStringToDate, Info.GetAttribute(attrDate)), ex)
        End Try
    End Sub
End Class